<?php
class ModelCatalogTopic extends Model {
	
	public function getTopics($filter = null) {
		$sql = "SELECT t.*, td.title FROM @@topic t LEFT JOIN @@topic_description td ON t.topic_id=td.topic_id";
		$sql .= " WHERE td.language_id = " . (int)C('config_language_id');
		$sql .= $filter->sort('t.topic_id', 'DESC');
		$sql .= $filter->limit();
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalTopics($filter = null) {
		return $this->db->queryOne("SELECT COUNT(*) as total FROM @@topic");
	}
	
	public function getTopic($topic_id) {
		global $V5_TIME;
		$date = date('Y-m-d', $V5_TIME);
		return $this->db->queryOne("SELECT t.*, title,description,seo_title,meta_keywords,meta_description FROM @@topic t JOIN @@topic_description td ON t.topic_id = td.topic_id WHERE status = 1 AND t.topic_id = $topic_id AND t.date_start <= '$date' AND t.date_end >= '$date' AND td.language_id = " . C('config_language_id'));
	}
		
	public function getTopicModules($topic_id) {
		$query = $this->db->query("SELECT * FROM @@topic_module WHERE topic_id = $topic_id ORDER BY sort_order, id");
		$modules = $query->rows;
		foreach ($modules as $k => $module) {
			if ($module['type'] == 'banner' || $module['type'] == 'slideshow') {
				$modules[$k]['data'] = @unserialize($module['data']);
			}
			elseif ($module['type'] == 'product') {
				$modules[$k]['data'] = @unserialize($module['data']);
				$modules[$k]['data']['title'] = $this->db->queryArray("SELECT * FROM @@topic_module_description WHERE id = {$module['id']}", 'language_id', 'data');
			}
			elseif ($module['type'] == 'custom') {
				$modules[$k]['data'] = $this->db->queryArray("SELECT * FROM @@topic_module_description WHERE id = {$module['id']}", 'language_id', 'data');
			}
		}
		return $modules;
	}
}